-- stuff

local luasocket = require 'socket'
local sock = assert(luasocket.udp())
assert(sock:setsockname('*', 12001))
sock:settimeout(nil)

while true do
  msg, src_ip, src_port = sock:receivefrom(100)

  if src_ip == '127.0.0.1' and msg:match('^quit.?$') then
    break
  end

  if msg:match('^i am %d+') then
    id = tonumber(msg:match('(%d+)'))
    if id == 1961 then
      fp = assert(io.open('home', 'a'))
      sock:sendto('ok\thome\n', src_ip, src_port)
    elseif id == 1945 then
      fp = assert(io.open('granny', 'a'))
      sock:sendto('ok\tgranny\n', src_ip, src_port)
    else
      fp = assert(io.open('other', 'a'))
    end
    fp:write(string.format("[%s]\t%s:%d\tID: %d\n", os.date('%b %d %T'), src_ip, src_port, id))
    fp:flush()
    fp:close()
  end
end

sock:close()

-- vim: set et sw=2 sts=2:
